package com.example.tsproject.Controller;

import com.example.tsproject.entity.QuestionDetails;
import com.example.tsproject.entity.Questions;
import com.example.tsproject.entity.Result;
import com.example.tsproject.service.AnalysisStudentsService;
import com.example.tsproject.service.AssignmentsService;
import com.example.tsproject.service.QuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.example.tsproject.Utils.ThreadLocalUtil;

import javax.sound.midi.Track;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/questions")
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private AssignmentsService assignmentsService;
    @Autowired
    private AnalysisStudentsService analysisStudentsService;

    // Generate questions based on chapter and quantity
    @GetMapping("/generate")
    public Result generateQuestions(
            @RequestParam String name,
            @RequestParam("questionAmount") String quantity,
            @RequestParam String chapterRange,
            @RequestParam  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)Date startTime,
            @RequestParam  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)Date endTime) {
        System.out.println(name);
        System.out.println(quantity);
        System.out.println(chapterRange);
        System.out.println(startTime);
        System.out.println(endTime);
        List<Integer> chapterList = new ArrayList<>();
        String[] chapterRangeArray = chapterRange.split(",");
        for (String chapter : chapterRangeArray) {
            chapterList.add(Integer.parseInt(chapter));
        }
        boolean isexistname=assignmentsService.checknameexist(name);
        if(isexistname){
            return Result.error("该名称已存在");
        }
        List<Questions> questions = questionsService.generateQuestions(chapterList, quantity);
//        String publisherNumber = "22222222";
        String publisherNumber = ThreadLocalUtil.get();
        boolean isCreated = assignmentsService.createAssignmentForStudent(publisherNumber, name, "type3", questions, startTime, endTime);
        if (questions != null && !questions.isEmpty() && isCreated) {
            return Result.success(questions);
        } else {
            return Result.error("No questions found");
        }
    }

    // Generate questions based on chapter and quantity
    @GetMapping("/generateTests")
    public Result generateTests(
            @RequestParam String name,
            @RequestParam("questionAmount") String quantity,
            @RequestParam String chapterRange,
            @RequestParam  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)Date startTime,
            @RequestParam  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)Date endTime) {
        System.out.println(name);
        System.out.println(quantity);
        System.out.println(chapterRange);
        System.out.println(startTime);
        System.out.println(endTime);
        List<Integer> chapterList = new ArrayList<>();
        String[] chapterRangeArray = chapterRange.split(",");
        for (String chapter : chapterRangeArray) {
            chapterList.add(Integer.parseInt(chapter));
        }
        List<Questions> questions = questionsService.generateQuestions(chapterList, quantity);
//        String publisherNumber = "22222222";
        String publisherNumber = ThreadLocalUtil.get();
        boolean isCreated = assignmentsService.createAssignmentForStudent(publisherNumber, name, "type4", questions, startTime, endTime);
        if (questions != null && !questions.isEmpty() && isCreated) {
            return Result.success(questions);
        } else {
            return Result.error("No questions found");
        }
    }




    @PostMapping("/assignments")
    public Result createAssignment(@RequestParam String name,
                                   @RequestParam("questionAmount") String quantity,
                                   @RequestParam String chapterRange,
                                   @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd%20HH:mm:ss") Date startTime,
                                   @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd%20HH:mm:ss") Date endTime,
                                   @RequestBody List<Questions> questions) {
        String publisherNumber = "22222222";
        boolean isCreated = assignmentsService.createAssignmentForStudent(publisherNumber, name, "type3", questions, startTime, endTime);
        if (isCreated) {
            return Result.success("Assignment created successfully");
        } else {
            return Result.error("Failed to create assignment");
        }
    }
    @GetMapping("/assignment/{assignmentId}")
    public Result getQuestionsByAssignmentId(@PathVariable Integer assignmentId) {
        List<Questions> questions = questionsService.getQuestionsByAssignmentId(assignmentId);
        if (questions != null && !questions.isEmpty()) {
            return Result.success(questions);
        } else {
            return Result.error("No questions found for this assignment");
        }
    }
    @GetMapping("/assignment_review/{assignmentId}")
    public Result getDetailedQuestionsByAssignmentIdAndStudentId(@PathVariable Integer assignmentId) {

        String studentId = "22222222";
        studentId= ThreadLocalUtil.get();
        System.out.println(assignmentId);
        List<QuestionDetails> questionDetails = questionsService.getDetailedQuestionsByAssignmentIdAndStudentId(assignmentId, studentId);
        System.out.println(questionDetails.size());
        if (questionDetails != null && !questionDetails.isEmpty()) {
            return Result.success(questionDetails);
        } else {
            return Result.error("No questions found for this assignment and student");
        }
    }

    @GetMapping("/analysis/{assignmentId}")
    public List<Map<String, Object>> getChapterAccuracy(@PathVariable Integer assignmentId) {
        return analysisStudentsService.AnalysisStudents(assignmentId);
    }

    @GetMapping("/assignToClass")
    public Result assignToClass(@RequestParam String name,
                                @RequestParam("questionAmount") String quantity,
                                @RequestParam String chapterRange,
                                @RequestParam("stuclass") String stuclass,
                                @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startTime,
                                @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
//        @RequestParam("stuclass") String stuclass,
        System.out.println(name);
        System.out.println(quantity);
        System.out.println(chapterRange);
        System.out.println(startTime);
        System.out.println(endTime);
//        System.out.println(stuclass);
//        String stuclass="222082";
        List<Integer> chapterList = new ArrayList<>();
        String[] chapterRangeArray = chapterRange.split(",");
        for (String chapter : chapterRangeArray) {
            chapterList.add(Integer.parseInt(chapter));
        }
        List<Questions> questions = questionsService.generateQuestions(chapterList, quantity);
        boolean isAssigned = assignmentsService.assignToClass("type1",name, questions, stuclass, startTime, endTime);
        if (isAssigned) {
            return Result.success("Assignments created successfully for class " + stuclass);
        } else {
            return Result.error("Failed to create assignments for class " + stuclass);
        }
    }
    @GetMapping("/generatefromteacher")
    public Result assignToClass1(@RequestParam String name,
                                @RequestParam("questionAmount") String quantity,
                                @RequestParam String chapterRange,
                                @RequestParam("stuclass") String stuclass,
                                @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startTime,
                                @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
//        @RequestParam("stuclass") String stuclass,
        System.out.println(name);
        System.out.println(quantity);
        System.out.println(chapterRange);
        System.out.println(startTime);
        System.out.println(endTime);
//        System.out.println(stuclass);
//        String stuclass="222082";
        List<Integer> chapterList = new ArrayList<>();
        String[] chapterRangeArray = chapterRange.split(",");
        for (String chapter : chapterRangeArray) {
            chapterList.add(Integer.parseInt(chapter));
        }
        List<Questions> questions = questionsService.generateQuestions(chapterList, quantity);
        boolean isAssigned = assignmentsService.assignToClass("type2",name, questions, stuclass, startTime, endTime);
        if (isAssigned) {
            return Result.success("Assignments created successfully for class " + stuclass);
        } else {
            return Result.error("Failed to create assignments for class " + stuclass);
        }
    }
}
